package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.domain.dto.requirement.AddRequirementRequest;
import com.zh.domain.dto.requirement.PageReqDynamicRequest;
import com.zh.domain.dto.requirement.UpdateRequirementRequest;
import com.zh.domain.dto.task.PageTaskDynamicRequest;
import com.zh.domain.vo.requirement.ReqDynamicVo;
import com.zh.domain.vo.requirement.RequirementVo;
import com.zh.domain.vo.task.TaskDynamicVo;
import com.zh.exception.ThrowUtils;
import com.zh.service.RequirementsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "RequirementController", value = "需求控制器")
@RestController
@RequestMapping("/requirement")
@Slf4j
@SaCheckLogin
public class RequirementController {
    @Resource
    private RequirementsService requirementsService;

    @PostMapping("/add")
    @ApiOperation(value = "addRequirement", notes = "新建需求")
    public BaseResponse<Boolean> addRequirement(@RequestBody AddRequirementRequest addRequirementRequest) {
        ThrowUtils.throwIf(null == addRequirementRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = requirementsService.addRequirement(addRequirementRequest);
        return ResultUtils.success(result);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateRequirement", notes = "更新需求")
    public BaseResponse<Boolean> updateRequirement(@RequestBody UpdateRequirementRequest updateRequirementReuqest) {
        ThrowUtils.throwIf(null == updateRequirementReuqest, ErrorCode.PARAMS_ERROR);
        Boolean result = requirementsService.updateRequirement(updateRequirementReuqest);
        return ResultUtils.success(result);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = ("deleteRequirement"), notes = "删除需求")
    public BaseResponse<Boolean> deleteRequirement(Integer id) {
        ThrowUtils.throwIf(null == id, ErrorCode.PARAMS_ERROR);
        Boolean result = requirementsService.removeById(id);
        return ResultUtils.success(result);
    }
    @GetMapping
    @ApiOperation(value = "getRequirement", notes = "id获取需求详情")
    public BaseResponse<RequirementVo> getRequirement(Integer id) {
        ThrowUtils.throwIf(null == id, ErrorCode.PARAMS_ERROR);
        RequirementVo result = requirementsService.getDetail(id);
        return ResultUtils.success(result);
    }

    @GetMapping("/getRequirementByTaskId")
    @ApiOperation(value = "getRequirementByTaskId", notes = "根据任务id获取需求信息")
    public BaseResponse<RequirementVo> getRequirementByTaskId(Integer taskId){
        ThrowUtils.throwIf(null == taskId, ErrorCode.PARAMS_ERROR);
        RequirementVo requirementVo= requirementsService.getByTaskId(taskId);
        return ResultUtils.success(requirementVo);
    }
    @GetMapping("/listRequirementByIterationId")
    @ApiOperation(value = "listRequirementByIterationId", notes = "根据迭代id获取需求列表")
    public BaseResponse<List<RequirementVo>> listRequirementByIterationId(Integer iterationId){
        ThrowUtils.throwIf(null == iterationId, ErrorCode.PARAMS_ERROR);
        List<RequirementVo> result = requirementsService.listByIterationId(iterationId);
        return ResultUtils.success(result);
    }

    @PostMapping("/dynamics/page")
    @ApiOperation(value = "pageTaskDynamics",notes = "分页获取任务动态")
    public BaseResponse<Page<ReqDynamicVo>> pageReqDynamic(@RequestBody PageReqDynamicRequest pageReqDynamicRequest){
        ThrowUtils.throwIf(null == pageReqDynamicRequest,ErrorCode.PARAMS_ERROR);
        Page<ReqDynamicVo> resultPage = requirementsService.pageDynamics(pageReqDynamicRequest);
        return ResultUtils.success(resultPage);
    }


}
